#include"Seqlist.h"

void InitSL(SL* sl)
{
	sl->array = NULL;
	sl->size = sl->capacity = 0;
}
void DestorySL(SL* sl)
{
	if (sl->array)
	{
		free(sl->array);
	}
	sl->array = NULL;
	sl->size = sl->capacity = 0;
}
void SLPrint(SL ps)
{
	for (int i = 0; i < ps.size; i++)
	{
		printf("%d ", ps.array[i]);
	}
	printf("\n");
}
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
	SLData* tmp = (SLData*)realloc(ps->array, newcapacity*sizeof(SLData));
	if (tmp == NULL)
	{
		perror("realloc false");
		exit(1);
	}
	ps->array = tmp;
	ps->capacity = newcapacity;
}
void SLPushFront(SL* ps, SLData x)
{
	assert(ps);
	SLCheckCapacity(ps);
	for (int i=ps->size;i>0;i--)
	{
		ps->array[i] = ps->array[i-1];
	}
	ps->array[0] = x;
	ps->size++;
}
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size; i++)
	{
		ps->array[i] = ps->array[i + 1];
	}
	ps->size--;
}
void SLPushBack(SL* ps, SLData x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->array[ps->size++] = x;
}
void SLPopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}